package org.internna.expensetracker.mvc;

import org.internna.expensetracker.util.SecurityUtils;
import org.internna.expensetracker.services.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.security.authentication.AuthenticationManager;

@Controller
@RequestMapping("/users/**")
public class UserController {

	@Autowired private UserService userService;
	@Autowired private AuthenticationManager authenticationManager;

	@RequestMapping(value = "register", method = RequestMethod.GET)
    public String register() {
		return "users/register";
    }

	@RequestMapping(value = "register", method = RequestMethod.POST)
    public String register(String username, String password, String name, String email) {
		userService.register(username, password, name, email);
		SecurityUtils.authenticate(authenticationManager, username, password);
		return "redirect:/financial/accounts/create";
    }

}
